package com.translate.web.config;

import java.text.SimpleDateFormat;
import java.util.TimeZone;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.Version;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.module.SimpleModule;

/**
 * JSON Object Mapper
 */
public class ResponseJsonMapper extends com.fasterxml.jackson.databind.ObjectMapper {

	/**
	 *
	 */
	private static final long serialVersionUID = 1808342056245750120L;

	public ResponseJsonMapper() {
		super();
		this.findAndRegisterModules();
		this.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
		this.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
//		this.configure(JsonGenerator.Feature.QUOTE_NON_NUMERIC_NUMBERS, true);
//		this.configure(JsonGenerator.Feature.WRITE_NUMBERS_AS_STRINGS, true);
		this.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
		this.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
		this.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);// 忽略未知属性

//        this.getSerializerProvider().setNullValueSerializer(new JsonSerializer<Object>() {
//            @Override
//            public void serialize(Object value, JsonGenerator jg, SerializerProvider sp) throws IOException {
//                jg.writeString(StringUtils.EMPTY);
//            }
//        });
		// 空List返回[],其他返回""
//        this.setSerializerFactory(this.getSerializerFactory().withSerializerModifier(new CustomerBeanSerializerModifier()));
		this.setTimeZone(TimeZone.getDefault());
		this.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));// 日期格式化输出
		SimpleModule module = new SimpleModule("ResponseModel", new Version(1, 0, 0, null, null, null));
		module.addSerializer(Boolean.class, new BooleanSerializer());
		module.addDeserializer(String.class, new StringDeserializer());
		registerModule(module);
	}
}